import 'package:flutter/material.dart';
import 'package:flutterclue/utils/SharePreferensUtil.dart';
import 'package:flutterclue/utils/UtilsString.dart';
import 'package:flutterclue/view/UserFragmentPopWindowTest.dart';

class ClueStatisticsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new ClueStatisticsPageState();
  }
}

class ClueStatisticsPageState extends State<ClueStatisticsPage> {
  final List<String> originData =
      new List.unmodifiable(["今年", "上年", "本月", "上月", "今日", "昨天"]);
  final List<String> originDatas = new List(5);
  bool _clickStoreTabState = true; // 店内线索 false代表未选中  true 代表选中
  bool _clickClueTabState = false; // 抢单 false代表未选中  true 代表选中
  bool _clueClassify = false; //控制时间选择框和线索分类的显示
  static String timestamp = UtilsString().timeFormat(new DateTime.now(), "day");
  String _selectedStartTime = timestamp.toString();
  String _selectedEndTime = timestamp.toString();
  String _selectedTag = "店内";

  void _clickTab() {
    setState(() {
      if (_selectedTag == "店内") {
        //店内线索
        if (_clickStoreTabState) {
          _clickStoreTabState = true;
          _clickClueTabState = false;
        } else {
          _clickStoreTabState = true;
          _clickClueTabState = false;
        }
      } else {
        //抢单线索
        if (_clickClueTabState) {
          _clickClueTabState = true;
          _clickStoreTabState = false;
        } else {
          _clickClueTabState = true;
          _clickStoreTabState = false;
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    _roleCheck();
    return Container(
      child: SingleChildScrollView(
          padding: EdgeInsets.all(16.0),
          child: new Center(
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    new Offstage(
                        offstage: false,
                        child: Container(
                          height: 52,
                          child: new PopupTypeButton(
                            originData: [
                              "上年",
                              "今日",
                              "昨日",
                              "本周",
                              "本月",
                              "上周",
                              "上月",
                              "本年",
                            ],
                          ),
                        )),
//                    Image(
//                      image: AssetImage("images/down_date.png"),
//                      width: 20.0,
//                      height: 20.0,
//                    ),
                    Offstage(
                        offstage: _clueClassify,
                        //将要包裹时间选择控件
                        child: Row(
                          children: <Widget>[
                            FlatButton(
                              padding: EdgeInsets.only(left: 5),
                              child: Text(
                                _selectedStartTime,
                              ),
                              onPressed: () {
                                selectedTime("_selectedStartTime");
                              },
                            ),
                            new Text("至"),
                            FlatButton(
                              padding: EdgeInsets.only(left: 5, right: 5),
                              child: Text(
                                _selectedEndTime,
                              ),
                              onPressed: () {
                                selectedTime("_selectedEndTime");
                              },
                            ),
                          ],
                        )),
                    Expanded(child: new Align(
                      child: Offstage(
                        offstage: !_clueClassify,
                        child: new Container(
                          child: Row(
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: <Widget>[
                              new Padding(
                                padding: new EdgeInsets.only(
                                    left: 0, top: 0, right: 10, bottom: 0),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: <Widget>[
                                    FlatButton(
                                      child: Text(
                                        "店内线索",
                                        style:
                                        setSelectedStyle(_clickStoreTabState),
                                      ),
                                      onPressed: () {
                                        _selectedTag = "店内";
                                        _clickTab();
                                      },
                                    ),
                                    Container(
                                      color: _clickStoreTabState
                                          ? Colors.blue
                                          : Colors.white,
                                      width: 40.0,
                                      height: 1.0,
                                    ),
                                  ],
                                ),
                              ),
                              new Padding(
                                padding: new EdgeInsets.only(
                                    left: 0, top: 0, right: 0, bottom: 0),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: <Widget>[
                                    FlatButton(
                                      child: Text(
                                        "抢单线索",
                                        style:
                                        setSelectedStyle(_clickClueTabState),
                                      ),
                                      onPressed: () {
                                        _selectedTag = "抢单";
                                        _clickTab();
                                      },
                                    ),
                                    Container(
                                      color: _clickClueTabState
                                          ? Colors.blue
                                          : Colors.white,
                                      width: 40.0,
                                      height: 1.0,
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ))

                  ],
                ),
                Row(children: <Widget>[
//                  new Offstage(
//                      offstage: false,
//                      child: Container(
//                        height: 52,
//                        child: new PopupTypeButton(
//                          originData: [
//                            "1",
//                            "2",
//                            "3", "1",
//                            "2",
//                            "3", "1",
//                            "2",
//                            "3",
//                          ],
//                        ),
//                      ))
                ],
//        new CustomPopupButton<List<String>>(
//            name: "全部",
//            child: new PopupTypeWindow(
//              originData: originData,
//              selectData: originDatas,
//            ),
//            fullWidth: true,
////                    arrows: new List.unmodifiable(["今年","上年","本月","上月","今日","昨天"]),
//            offset: new Offset(0.0, 0.0)),

                    //List
//              new SliverFixedExtentList(
//                itemExtent: 50.0,
//                delegate: new SliverChildBuilderDelegate(
//                        (BuildContext context, int index) {
//                      //创建列表项
//                      return new Container(
//                        alignment: Alignment.center,
//                        color: Colors.lightBlue[100 * (index % 9)],
//                        child: new Text('list item $index'),
//                      );
//                    }, childCount: 50 //50个列表项
//                ),
                    ),
              ],
            ),
          )),
    );
  }

  TextStyle setSelectedStyle(bool isUser) {
    TextStyle style = null;
    if (isUser) {
      style = new TextStyle(
          inherit: false,
          color: Colors.blue,
          fontSize: 16.0,
          height: 1.0,
          fontFamily: "Courier",
          decoration: TextDecoration.none,
          decorationStyle: TextDecorationStyle.solid);
    } else {
      style = new TextStyle(
          inherit: false,
          color: Colors.black,
          fontSize: 16.0,
          height: 1.0,
          fontFamily: "Courier",
          decoration: TextDecoration.none,
          decorationStyle: TextDecorationStyle.solid);
    }
    return style;
  }

  Future selectedTime(String selectedresult) async {
    await showDatePicker(
      context: context,
      initialDate: new DateTime.now(),
      firstDate: new DateTime.now().subtract(new Duration(days: 30)), // 减 30 天
      lastDate: new DateTime.now().add(new Duration(days: 30)), // 加 30 天
    ).then((DateTime val) {
      timestamp = UtilsString().timeFormat(val, "day");
      if (selectedresult == "_selectedStartTime") {
        _selectedStartTime = timestamp;
      } else if (selectedresult == "_selectedEndTime") {
        _selectedEndTime = timestamp;
      }
    }).catchError((err) {
    });
    UtilsString.getAuthority("");
    _clickTab();
  }

   /// 角色校验
  _roleCheck() async {
    int role = await SharePreferencesUtil.getInt(SharePreferencesUtil.RoleType);
    if (role != 3 && role != 7) {
      _clueClassify = true;
      if (await UtilsString.getAuthority("reception_notice")
          || await UtilsString.getAuthority("reception_notice")
          || await UtilsString.getAuthority("reception_notice") ) {
        //有店内线索

        //判断有没有抢单权限
        if (await UtilsString.getAuthority("graborder")) {}
      }
    } else {
      _clueClassify = false;
    }
  }
}
